package com.torchclx.etcdstarter;

import io.etcd.jetcd.Client;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisKeyValueAdapter;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@EnableConfigurationProperties(EtcdProperties.class)
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class EtcdRedisConfig {

    @Bean
    public Client client (EtcdProperties etcdProperties) {
        return Client.builder().endpoints(etcdProperties.getHost()).build();
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(Client client) {
        return new Etcd2RedisAdaptor(new EtcdValueOperations(client));
    }

    @Bean
    public RedisKeyValueAdapter redisKeyValueAdapter() {
        return new VoidRedisKeyValueAdapter();
    }
}
